// myviews/money/money.js
var app = getApp();
let servicePath = require("../../utils/servicePath.js");
let alert = require("../../utils/alert.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sumMoney: 0.00,
    consumptionList: [],
    hiddenPay: true,
    payMoney: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  paymoney: function () {
    this.setData({
      hiddenPay: !this.data.hiddenPay
    })

  },
  changeMoney: function (e) {

    this.setData({
      payMoney: e.detail.value
    })
  },
  //取消按钮  
  cancelPay: function () {
    this.setData({
      hiddenPay: true
    });
  },
  //确认  
  confirmPay: function (e) {
    var regNum = new RegExp('[0-9]', 'g');
    var rsNum = regNum.exec(this.data.payMoney);
    if (rsNum == null) {
      alert.toast("请输入数字");
    } else {
      this.setData({
        hiddenPay: true
      });

      //开启微信支付

      // 登录
      wx.login({
        success: res => {
          var loginInfo = res;
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          if (loginInfo.code) {
            wx.request({
              url: 'https://api.weixin.qq.com/sns/jscode2session',
              data: {
                appid: "wxb74c4c6e59fb317e",
                secret: "da39ff39e91e87771b971268ee2af331",
                js_code: loginInfo.code,
                grant_type: " authorization_code"
              },
              success: function (res) {

                let _openId = res.data.openid;
                console.log(_openId);
              },
              fail: function (err) {
                console.log(err);
              }
            })
          }
        }
      })

      var backUserInfo = app.globalData.backUserInfo;
      var _this = this;
      if (backUserInfo == null) {
        wx.redirectTo({
          url: '../register/register',
        })
      } else {
        // console.log(rsNum.input);
        // var AP0301 = backUserInfo.syS0100;
        // var AP0302 = this.data.payMoney;
        // var AP0303 = "通过微信支付充值";

        // wx.request({
        //   url: servicePath.AccountService(),
        //   data: {
        //     AP0301: AP0301,
        //     AP0302: AP0302,
        //     AP0303: AP0303
        //   },
        //   method: "POST",
        //   success: function (res) {
        //     console.log(res);
        //     if (res.data.code == 200) {
        //       _this.loadData();
        //     }
        //   }
        // })
      }
    }

  },
  ///加载数据
  loadData: function () {
    var userId = app.globalData.backUserInfo.syS0100;

    var _this = this;
    wx.request({
      url: servicePath.AccountService() + "?userid=" + userId,
      method: "GET",
      success: function (res) {
        console.log(res);
        if (res.data.code == 200 && res.data.condition != null) {

          _this.setData({
            consumptionList: res.data.condition.data,
            sumMoney: res.data.condition.aP0302
          })
        } else {
          _this.setData({
            consumptionList: [],
            sumMoney: 0.00
          })
        }
      }
    })
  }
})